package com.neusoft.yuejian26.yuejian_26.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neusoft.yuejian26.yuejian_26.Activity.AboutActivity;
import com.neusoft.yuejian26.yuejian_26.Activity.QRActivity;
import com.neusoft.yuejian26.yuejian_26.Other.CacheInfo;
import com.neusoft.yuejian26.yuejian_26.Activity.FeedBackActivity;
import com.neusoft.yuejian26.yuejian_26.Activity.LoginActivity;
import com.neusoft.yuejian26.yuejian_26.Activity.MyShareActivity;
import com.neusoft.yuejian26.yuejian_26.Activity.UserIntroActivity;
import com.neusoft.yuejian26.yuejian_26.Bean.Userbean;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.UserModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment {

    public TextView name_me,phone_me;
    public LinearLayout intro_me,qr_me,post_me,attention_me,feedback_me,btn_about,btn_quit;
    public ImageView ic_QR;
    public CircleImageView pic_me;
    public View view = null;
    String uid=null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_me, container, false);
        initview();
        initEvent();
        getUserSimpData();
        return view;
    }

    private void getUserSimpData(){
        uid=new CacheInfo(getActivity()).getUserIdCache();
        if(uid!=null) {
            UserModel userModel = new UserModel();
            Call call = userModel.userIntro(uid);
            userModel.bindCallback(call, new RetrofitCallback() {
                @Override
                public void onResponse(Object o) {
                    Userbean userinfodata = (Userbean) o;
                    Picasso.with(getContext()).load("http://115.159.93.244/new18yuejian/Public/upload/" + userinfodata.getUserpic()).into(pic_me);
                    name_me.setText(userinfodata.getUname());
                    phone_me.setText(userinfodata.getUtel());
                }
            });

        }
    }

    private void initview(){
        intro_me=(LinearLayout) view.findViewById(R.id.intro_me);
        post_me= (LinearLayout) view.findViewById(R.id.post_me);
        name_me=(TextView) view.findViewById(R.id.name_me);
        phone_me=(TextView) view.findViewById(R.id.phone_me);
        qr_me= (LinearLayout) view.findViewById(R.id.qr_me);
        attention_me= (LinearLayout) view.findViewById(R.id.attention_me);
        feedback_me= (LinearLayout) view.findViewById(R.id.feedback_me);
        btn_about= (LinearLayout) view.findViewById(R.id.btn_about);
        ic_QR=(ImageView) view.findViewById(R.id.ic_QR);
        pic_me=(CircleImageView)view.findViewById(R.id.pic_me);
        btn_quit=(LinearLayout)view.findViewById(R.id.btn_quit);
    }


    private void initEvent() {
        intro_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), UserIntroActivity.class);
                startActivity(intent);
            }
        });
        qr_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),QRActivity.class);
                startActivity(intent);
            }
        });
        post_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),MyShareActivity.class);
                startActivity(intent);
            }
        });
        feedback_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),FeedBackActivity.class);
                startActivity(intent);
            }
        });
        btn_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),AboutActivity.class);
                startActivity(intent);
            }
        });
        btn_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), LoginActivity.class);
                new CacheInfo(getActivity()).setUserIdCache(null);
                startActivity(intent);
                getActivity().finish();

            }
        });
    }

}
